import Vue from 'vue'
import VueRouter from 'vue-router'
import api from '../api/api'
// import antennaFau from '../components/antennaFau.vue'
// import elecarFau from '../components/elecarFau.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/dashboard',
    component: () => import('../views/dashboard.vue'),
    meta: { title: '首页' }
  },
  {
    path: '/faultmodel/:uuid/:pheno',
    name: 'faultmodel',
    component: () => import('../components/fauTest.vue'),
    meta: { title: ' ' }
  }
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  document.title = `${to.meta.title}`
  next()
  const carType = localStorage.getItem('type')
  if (!carType && to.path !== '/dashboard') {
    return next({ path: '/dashboard' })
  }
  if (to.path === '/dashboard') {
    localStorage.clear()
    // console.log(routes)
    // eslint-disable-next-line no-unused-vars
  }
  if (to.path === '/') {
    api.post('/getMenu', carType).then(res => {
      // console.log(res)
      const childRoute = res.data.map(item => {
        return {
          path: `/${item.navPath}`,
          component: () => import(`../components/${item.navPath}.vue`),
          meta: { title: `${item.type}` }
        }
      })
      const rou = {
        name: 'home',
        path: '/',
        component: () => import('../views/Home.vue'),
        redirect: `/${res.data[0].navPath}`,
        children: childRoute
      }
      router.addRoute(rou)
      console.log(router)
      next({ ...to, replace: true })
    })
  }
  if (to.name === 'faultmodel') {
    document.title = to.params.pheno
    next()
  }
})

export function resetRouter () {
  const newRouter = new VueRouter({
    mode: 'hash',
    base: process.env.BASE_URL,
    routes
  })
  router.matcher = newRouter.matcher
}
export default router
